<?PHP
	
	include("../../includes/configAjax.php");

	$usernameTaken = false;
	$emailTaken = false;
	$error = false;
	$errorText = "";

	$desiredUname = $db->prep($_POST['uname']);
	$desiredUname = strtolower($desiredUname);
	$result = $db->query("SELECT * FROM ".$prefix."users WHERE username_clean='".$desiredUname."'");
	if(mysql_num_rows($result)>0 || strlen($desiredUname)<3){
		$usernameTaken = true;	
		$errorText .= " Username taken";
	} else {
		$usernameTaken = false;
	}
	
	$desiredEmail = $db->prep($_POST['email']);
	$result = $db->query("SELECT * FROM ".$prefix."users WHERE email='".$desiredEmail."'");
	if(mysql_num_rows($result)>0 || strlen($desiredEmail)<3){
		$emailTaken = true;	
		$errorText .= " Email taken";
	} else {
		$emailTaken = false;
	}

	if($_POST['email'] != $_POST['email_con']){
		$error = true;
		$errorText .= " Email addresses do not match";
	}
	
	if($_POST['pword'] != $_POST['pword_con']){
		$error = true;
		$errorText .= " Passwords do not match";
	}

	if( preg_match('/\W/', $_POST['pword']) || preg_match('/\W/', $_POST['pword_con']) || preg_match('/\W/', $_POST['uname']) || preg_match('/\W/', $desiredUname) || preg_match('/\W/', $_POST['rname']) ){
		$error = true;
		$errorText .= " All fields can only contain a-z and 0-9";
	}
	
	if(($usernameTaken==false) && ($emailTaken==false) && ($error==false)){
		$outcome = $users->reg($db, $config, $_POST['uname'], $desiredUname, $_POST['rname'], $_POST['email'], $_POST['pword']);

		if($outcome){
			echo "ok";	
		} else {
			echo $outcome;	
		}
	} else {
		echo $errorText;
	}

?>